package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Category;
import com.example.domain.Dish;
import com.example.domain.DishFlavor;
import com.example.domain.Employee;
import com.example.dto.DishDto;
import com.example.service.CategoryService;
import com.example.service.DishFlavorService;
import com.example.service.DishService;
import com.example.mapper.DishMapper;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【dish(菜品管理)】的数据库操作Service实现
* @createDate 2022-04-30 10:08:57
*/
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService{

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Resource
    private DishMapper dishMapper;

    @Override
    public Page<Dish> getDishListForPage(Integer page, Integer pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page, pageSize);
        this.page(dishPage,new LambdaQueryWrapper<Dish>().like(!StringUtil.isNullOrEmpty(name),Dish::getName,name)
                .orderByAsc(Dish::getSort)
                .orderByDesc(Dish::getUpdateTime));
        return dishPage;
    }

    @Override
    public Page<DishDto> dishToDishDto(Page<Dish> dishPage) {
        Page<DishDto> dishDtoPage = new Page<>();
        //映射除records属性中的数据
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");

        List<DishDto> collect = dishPage.getRecords().stream().map((data) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(data, dishDto);

            Long categoryId = data.getCategoryId();
            String name = categoryService.getById(categoryId).getName();
            dishDto.setCategoryName(name);
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(collect);
        return dishDtoPage;
    }

    @Override
    public boolean deleteByIds(Long[] ids) {
        List<Long> longs = Arrays.asList(ids);
        boolean b = this.removeByIds(longs);
        for (Long dishId : longs) {
            dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId,dishId));
        }
        return b;
    }

    @Override
    public void dishStatusByStatus(Integer status, Long[] ids, HttpSession session) {
        Long id1 = (Long) session.getAttribute("employee");
        for (Long id : ids) {
            boolean update = this.update(new LambdaUpdateWrapper<Dish>()
                    .set(Dish::getStatus, status)
                    .set(Dish::getUpdateUser,id1)
                    .set(Dish::getUpdateTime, LocalDateTime.now())
                    .eq(Dish::getId, id));
        }
    }

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        List<DishFlavor> flavors = dishDto.getFlavors();

        try {
            dishMapper.insert(dish);
            System.out.println(dish.getId());
            flavors =flavors.stream().map((date)->{
                date.setDishId(dish.getId());
                return date;
            }).collect(Collectors.toList());
            dishFlavorService.saveBatch(flavors);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, id));
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        List<DishFlavor> flavors = dishDto.getFlavors();

        try {
            this.updateById(dish);
            dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId,dish.getId()));
            flavors = flavors.stream().map((item) -> {
                item.setDishId(dishDto.getId());
                return item;
            }).collect(Collectors.toList());
            dishFlavorService.saveOrUpdateBatch(flavors);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}




